<template>
  <div>
    <el-upload
      action="https://knowledge.nervenets.com/api/v1/file/upload"
      list-type="text"
      :on-remove="handleRemove"
      :on-success="handleAvatarSuccess"
      :before-upload="beforeAvatarUpload"
      :show-file-list="false"
      name="userfile"
    >
        <!-- <img style="margin-top: -1px;margin-left:-1px;width: 150px;height: 150px;border-radius: 6px" v-if="dialogImageUrl"  :src="dialogImageUrl" class="avatar"> -->
        <audio :src="dialogImageUrl" ref="audio" v-if="dialogImageUrl" :controls="dialogImageUrl"></audio>
        <i v-if="!dialogImageUrl" class="el-icon-plus avatar-uploader-icon"></i>
    </el-upload>
  </div>
</template>

<script>
export default {
  name: 'UploadImg',
  props: {
      duration:0,
      getTime:{
        type:Function,
        default:null
      },
      setName: {
        type: Function,
        default: null
      },
      setImg: {
        type: Function,
        default: null
      },
      dialogImageUrl:{
        type:String,
        default:''
      }
    },
  data() {
    return {
      myDialogImageUrl:this.dialogImageUrl
    };
  },
  methods: {
    handleRemove(file) {
      console.log(file);
    },
    handleAvatarSuccess(res){
      this.myDialogImageUrl=res.data
      this.setImg(res.data)
      setTimeout(()=>{
        let test = this.$refs.audio;
        setTimeout(()=>{
          console.log('ceshi',parseInt(test.duration))
          this.getTime(parseInt(test.duration))
        },1000)
      },1000)
     ;
    },
    beforeAvatarUpload(file) {
        console.log(file,'file')
        this.setName(file.name)
    },
    timeToMinute(times){
       var t;
        if(times > -1){
            var hour = Math.floor(times/3600);
            var min = Math.floor(times/60) % 60;
            var sec = times % 60;
            if(hour < 10) {
                t = '0'+ hour + ":";
            } else {
                t = hour + ":";
            }
 
            if(min < 10){t += "0";}
            t += min + ":";
            if(sec < 10){t += "0";}
            t += sec.toFixed(2);
        }
        t=t.substring(0,t.length-3);
        return t;
  
  }
  },created() {
  }
}
</script>
